package com.ruoyi.project.system.interact.service.impl;

import java.util.Date;
import java.util.List;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.security.ShiroUtils;
import com.ruoyi.project.system.user.domain.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.project.system.interact.mapper.DjGraphicCommentMapper;
import com.ruoyi.project.system.interact.domain.DjGraphicComment;
import com.ruoyi.project.system.interact.service.IDjGraphicCommentService;

/**
 * 绘声绘影评论Service业务层处理
 * 
 * @author ruoyi
 * @date 2023-07-01
 */
@Service
public class DjGraphicCommentServiceImpl implements IDjGraphicCommentService 
{
    @Autowired
    private DjGraphicCommentMapper djGraphicCommentMapper;

    /**
     * 查询绘声绘影评论
     * 
     * @param id 绘声绘影评论主键
     * @return 绘声绘影评论
     */
    @Override
    public DjGraphicComment selectDjGraphicCommentById(Long id)
    {
        return djGraphicCommentMapper.selectDjGraphicCommentById(id);
    }

    /**
     * 查询绘声绘影评论列表
     * 
     * @param djGraphicComment 绘声绘影评论
     * @return 绘声绘影评论
     */
    @Override
    public List<DjGraphicComment> selectDjGraphicCommentList(DjGraphicComment djGraphicComment)
    {
        return djGraphicCommentMapper.selectDjGraphicCommentList(djGraphicComment);
    }

    /**
     * 根据动态ID查询评论列表
     * 
     * @param dynamicId 动态ID
     * @return 评论集合
     */
    @Override
    public List<DjGraphicComment> selectCommentsByDynamicId(Long dynamicId)
    {
        return djGraphicCommentMapper.selectCommentsByDynamicId(dynamicId);
    }

    /**
     * 新增绘声绘影评论
     * 
     * @param djGraphicComment 绘声绘影评论
     * @return 结果
     */
    @Override
    public int insertDjGraphicComment(DjGraphicComment djGraphicComment)
    {
        djGraphicComment.setCreateTime(DateUtils.getNowDate());
        djGraphicComment.setCommentTime(DateUtils.getNowDate());
        djGraphicComment.setAuditFlag(0); // 默认待审核
        djGraphicComment.setDelFlag(0); // 默认未删除
        return djGraphicCommentMapper.insertDjGraphicComment(djGraphicComment);
    }

    /**
     * 修改绘声绘影评论
     * 
     * @param djGraphicComment 绘声绘影评论
     * @return 结果
     */
    @Override
    public int updateDjGraphicComment(DjGraphicComment djGraphicComment)
    {
        djGraphicComment.setUpdateTime(DateUtils.getNowDate());
        return djGraphicCommentMapper.updateDjGraphicComment(djGraphicComment);
    }

    /**
     * 批量删除绘声绘影评论
     * 
     * @param ids 需要删除的绘声绘影评论主键
     * @return 结果
     */
    @Override
    public int deleteDjGraphicCommentByIds(Long[] ids)
    {
        return djGraphicCommentMapper.deleteDjGraphicCommentByIds(ids);
    }

    /**
     * 删除绘声绘影评论信息
     * 
     * @param id 绘声绘影评论主键
     * @return 结果
     */
    @Override
    public int deleteDjGraphicCommentById(Long id)
    {
        return djGraphicCommentMapper.deleteDjGraphicCommentById(id);
    }

    /**
     * 审核评论
     * 
     * @param id 评论ID
     * @param auditFlag 审核标志（1-通过，2-拒绝）
     * @return 结果
     */
    @Override
    public int auditComment(Long id, Integer auditFlag)
    {
        DjGraphicComment comment = new DjGraphicComment();
        comment.setId(id);
        comment.setAuditFlag(auditFlag);
        comment.setUpdateTime(DateUtils.getNowDate());
        return djGraphicCommentMapper.updateDjGraphicComment(comment);
    }

    /**
     * 统计动态的评论数量
     * 
     * @param dynamicId 动态ID
     * @return 评论数量
     */
    @Override
    public int countCommentsByDynamicId(Long dynamicId)
    {
        return djGraphicCommentMapper.countCommentsByDynamicId(dynamicId);
    }

    /**
     * 批量审核评论
     * 
     * @param ids 评论ID数组
     * @param auditFlag 审核标志（1-通过，2-拒绝）
     * @return 结果
     */
    @Override
    public int batchAuditComments(Long[] ids, Integer auditFlag)
    {
        int result = 0;
        for (Long id : ids) {
            result += auditComment(id, auditFlag);
        }
        return result;
    }

    /**
     * 根据动态ID和用户权限查询评论列表
     * 普通用户只能看到审核通过的评论和自己发表的评论
     * 管理员可以看到所有评论
     * 
     * @param dynamicId 动态ID
     * @return 评论列表
     */
    @Override
    public List<DjGraphicComment> selectCommentsByDynamicIdWithPermission(Long dynamicId)
    {
        User currentUser = ShiroUtils.getSysUser();
        DjGraphicComment queryParam = new DjGraphicComment();
        queryParam.setDynamicId(dynamicId);
        
        // 如果是管理员，可以查看所有评论
        if (User.isAdmin(currentUser.getUserId()))
        {
            return djGraphicCommentMapper.selectDjGraphicCommentList(queryParam);
        }
        
        // 普通用户只能看到审核通过的评论和自己发表的评论
        queryParam.setCreateBy(currentUser.getUserName());
        return djGraphicCommentMapper.selectCommentsByDynamicIdWithPermission(queryParam);
    }
} 